from tortoise.models import Model
from tortoise import fields
from datetime import datetime
from typing import Optional


class User(Model):
    """用户模型"""
    
    id = fields.IntField(pk=True, description="用户ID")
    openid = fields.CharField(max_length=100, unique=True, description="微信openid")
    nickname = fields.CharField(max_length=100, null=True, description="昵称")
    avatar = fields.TextField(null=True, description="头像URL")
    phone = fields.CharField(max_length=20, null=True, description="手机号")
    points = fields.IntField(default=0, description="积分余额")
    
    # 打卡相关字段
    is_checkin = fields.BooleanField(default=False, description="今日是否已打卡")
    total_checkin_days = fields.IntField(default=0, description="总打卡天数")
    continuous_checkin_days = fields.IntField(default=0, description="当前连续打卡天数")
    last_checkin_date = fields.DateField(null=True, description="最后打卡日期")
    
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
    
    class Meta:
        table = "users"
        table_description = "用户表"
    
    def __str__(self):
        return f"User(id={self.id}, nickname={self.nickname})"